/*
 * CBulletDebugDrawer.cpp
 *
 *  Created on: 15.10.2012
 *      Author: Kirill Blinov
 *     License: LGPL
 */

#include "CBulletDebugDrawer.h"
#include <iostream>

CBulletDebugDrawer* CBulletDebugDrawer::instance = NULL;

CBulletDebugDrawer::CBulletDebugDrawer() : debugMode( DBG_DrawAabb )
{

}

CBulletDebugDrawer::~CBulletDebugDrawer()
{

}

void
CBulletDebugDrawer::drawLine(const btVector3& from,const btVector3& to,const btVector3& color)
{
	boost::mutex::scoped_lock Lock( internalMutex );
	objectsToDraw.push_back( DebugLine( from, to, color ) );
}

void
CBulletDebugDrawer::drawContactPoint(const btVector3& PointOnB,const btVector3& normalOnB,btScalar distance,int lifeTime,const btVector3& color)
{

}

void
CBulletDebugDrawer::reportErrorWarning(const char* warningString)
{
	fprintf( stderr, "%s\n",warningString );
}

void
CBulletDebugDrawer::draw3dText(const btVector3& location,const char* textString)
{
	fprintf( stdout,  "%s\n", textString );
}

void
CBulletDebugDrawer::setDebugMode(int DebugMode)
{
	debugMode = (DebugDrawModes)DebugMode;
}

int
CBulletDebugDrawer::getDebugMode() const
{
	return debugMode;
}

void
CBulletDebugDrawer::OpenGlDraw( glm::mat4 ProjectionMatrix, glm::mat4 ViewMatrix )
{
	boost::mutex::scoped_lock Lock( internalMutex );

	glMatrixMode( GL_PROJECTION );
	glLoadMatrixf( glm::value_ptr(ProjectionMatrix) );

	glMatrixMode( GL_MODELVIEW );
	glLoadMatrixf( glm::value_ptr(ViewMatrix) );

	glBegin(GL_LINES);
	for ( std::vector<DebugLine>::const_iterator Begin = objectsToDraw.begin(), End = objectsToDraw.end();
			Begin != End; Begin++ )
	{
		DebugLine Line = *Begin;
		glColor3f( Line.color.x(), Line.color.y(), Line.color.z() );
		glVertex3f( Line.firstPoint.x(), Line.firstPoint.y(), Line.firstPoint.z() );
		glVertex3f( Line.secondPoint.x(), Line.secondPoint.y(), Line.secondPoint.z() );
	}
	glEnd();
	objectsToDraw.clear();
}
